package com.hafu.view.ShangPin;
import com.hafu.model.SpInfo;

import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtl;

import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author YDD
 */
public class AddShangPinFrm extends JFrame {
    // 声明 SpInfoService 实例
    private SpInfoService spInfoService;

    public AddShangPinFrm() {
        spInfoService = new SpInfoService(); // 初始化 SpInfoService 实例
        initComponents();
    }

    private void button1(ActionEvent e) {

        String name = textField1.getText();
        if (StringUtl.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"商品名称不能为空");
            return;
        }
        // 验证商品名称只能包含中文字符 自己添加的判断
        if (!name.matches("[\\u4e00-\\u9fa5]+")) {
            JOptionPane.showMessageDialog(null, "商品名称只能包含中文字符");
            return;
        }
        if (StringUtl.isEmpty(textField2.getText())){
            JOptionPane.showMessageDialog(null,"商品进价不能为空");
            return;
        }
        if (StringUtl.isEmpty(textField3.getText())){
            JOptionPane.showMessageDialog(null,"商品售价不能为空");
            return;
        }
        BigDecimal price = new BigDecimal(textField2.getText());
        BigDecimal sell_price = new BigDecimal(textField3.getText());
        SpInfo spInfo = new SpInfo(name, price, sell_price);
        Boolean aBoolean = spInfoService.addSpInfo(spInfo); // 使用实例调用方法
        if (aBoolean){
            JOptionPane.showMessageDialog(null,"添加成功");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null,"添加失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5546\u54c1");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5546\u54c1\u540d\u5b57");

        //---- label2 ----
        label2.setText("\u5546\u54c1\u8fdb\u4ef7");

        //---- label3 ----
        label3.setText("\u5546\u54c1\u552e\u4ef7");

        //---- button1 ----
        button1.setText("\u63d0\u4ea4");
        button1.addActionListener(e -> button1(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(62, 62, 62)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(label1, GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                                        .addComponent(label3, GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(textField1, GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                                        .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                                        .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE))
                                .addContainerGap(94, Short.MAX_VALUE))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                .addContainerGap(207, Short.MAX_VALUE)
                                .addComponent(button1, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
                                .addGap(171, 171, 171))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label1))
                                .addGap(33, 33, 33)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label2))
                                .addGap(36, 36, 36)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label3))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(button1)
                                .addContainerGap(45, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
